﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading.Tasks;

namespace SynergyControls
{
    public class Bar : ControlBase
    {

        #region Delegates

        public delegate void Selector();
        public delegate void Deselector();

        #endregion Delegates

        #region Events

        public static event RoutedEventHandler Selected;
        public event RoutedEventHandler Deselected;

        public void RaiseSelectedEvent()
        {
            if (Selected != null)
            {
                Selected(this, new RoutedEventArgs());
            }
        }
        private void RaiseDeselectedEvent()
        {
            if (Deselected != null)
            {
                Deselected(this, new RoutedEventArgs());
            }
        }

        #endregion Events

        #region Fields
        private System.Threading.Thread SequencersThread;
        static List<Bar> theListOfBars = new List<Bar>();

        #endregion Fields

        #region Properties
        public bool barIsSelected
        {
            get { return (bool)GetValue(barIsSelectedProperty); }
            set { SetValue(barIsSelectedProperty, value); }
        }
        public ObservableCollection<RhythmButtonBar> RepresentedBarCollection
        {
            get;
            protected set;
        }
        public Rectangle ActualBar
        {
            get
            {
                try
                {
                    return ResourceRoot.FindName("PART_Bar") as Rectangle;
                }
                catch (NullReferenceException)
                {
                    return null;
                }
            }
        }
        public bool IsViable
        {
            get
            {
                if (RepresentedBarCollection != null)
                    return true;
                return false;
            }
        }
        #endregion Properties

        #region Dependency Properties

        #region IsSelected

        private static readonly DependencyProperty barIsSelectedProperty = DependencyProperty.Register("barIsSelected", typeof(bool), typeof(Bar), new PropertyMetadata(new PropertyChangedCallback(IsSelectedChanged)));
        private static void IsSelectedChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
        {
            Bar instance = dependency as Bar;
            if (instance != null)
            {
                switch ((bool)args.NewValue)
                {
                    case true:
                        instance.RaiseSelectedEvent();
                        break;
                    case false:
                        instance.RaiseDeselectedEvent();
                        break;
                }
            }
        }

        #endregion IsSelected


        #region BarStyle
        internal static readonly DependencyProperty BarStyleProperty = DependencyProperty.Register("BarStyle", typeof(Style), typeof(Bar), new PropertyMetadata());
        public Style BarStyle
        {
            get
            {
                return (Style)GetValue(BarStyleProperty);
            }
            set
            {
                SetValue(BarStyleProperty, value);
            }
        }
        #endregion

        #region BarSelectedStyle

        internal static readonly DependencyProperty BarSelectedStyleProperty = DependencyProperty.Register("BarSelectedStyle", typeof(Style), typeof(Bar), new PropertyMetadata());
        internal static void BarSelectedStyleChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
        {

        }

        public Style BarSelectedStyle
        {

            get
            {
                return (Style)GetValue(BarSelectedStyleProperty);

            }
            set
            {
                SetValue(BarSelectedStyleProperty, value);
            }
        }
        #endregion

        #endregion Dependency Properties

        #region Constructors
        static Bar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Bar), new FrameworkPropertyMetadata(typeof(Bar)));
            //System.Threading.Thread newSTAThread = new System.Threading.Thread(new System.Threading.ThreadStart());
        }
        public Bar(ObservableCollection<RhythmButtonBar> representedBar)
        {
            RepresentedBarCollection = representedBar;
            RepresentedBarCollection.CollectionChanged += RepresentedBarCollection_CollectionChanged;
        }


        public Bar()
        {
            theListOfBars.Add(this);
            Selected += Bar_Selected;
            Deselected += Bar_Deselected;
            Bar.Selected += Bar_Selected;
            SequencersThread = System.Threading.Thread.CurrentThread;
        }
        public override void OnApplyTemplate()
        {
            switch (barIsSelected)
            {
                case true:
                    ActualBar.Style = BarSelectedStyle;
                    break;
                case false:
                    ActualBar.Style = BarStyle;
                    break;
            }
        }
        #endregion Constructors

        #region Methods
        public void Select()
        {
            Dispatcher.FromThread(SequencersThread).Invoke(new Selector(select));
        }
        private void select()
        {
            if (!this.barIsSelected)
                this.barIsSelected = true;
            //else
                //this.barIsSelected = false;
        }
        public void Deselect()
        {
            Dispatcher.FromThread(SequencersThread).Invoke(new Deselector(deselect));
        }
        private void deselect()
        {
            this.barIsSelected = false;
        }
        #endregion Methods

        #region Event Handlers

        void RepresentedBarCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            System.Windows.MessageBox.Show("this is RepresentedBarCollection_CollectionChanged.  the property chat triggered the event is: " + e.Action.ToString());
            
        }
        void Bar_Deselected(object sender, RoutedEventArgs e)
        {
            ActualBar.Style = BarStyle;
        }
        void Bar_Selected(object sender, RoutedEventArgs e)
        {
            if ((sender as Bar) == this)
            {
                if (ActualBar.Style != BarSelectedStyle)
                    ActualBar.Style = BarSelectedStyle;
            }
            else
                Deselect();
        }

        #endregion event Handlers
    }
}
